#!/bin/bash

SCAN_TIMEOUT=1

test -f /usr/bin/bluetoothctl && (

# bluetoothctl exist, then do the scan test
bluetoothctl --timeout=$SCAN_TIMEOUT power on
# Remove device list
for BTDEV in $(bluetoothctl --timeout=$SCAN_TIMEOUT devices|grep 'Device'|awk '{ print $2 }')
do 
	#echo "remove $BTDEV"
	bluetoothctl --timeout=$SCAN_TIMEOUT remove $BTDEV > /dev/null
done
# Scan BT devices
bluetoothctl --timeout=$SCAN_TIMEOUT scan on
# Check number of device list
NUM_DEV=$(bluetoothctl --timeout=$SCAN_TIMEOUT devices|grep 'Device'|awk '{ print $2 }'|wc -l)
if [[ $NUM_DEV -eq 0 ]]; then
	echo "FAIL: $NUM_DEV"
else
	echo "PASS: $NUM_DEV"
fi


# bluetoothctl don't exist, then show error and exit 
) || echo "$0: ERROR"
